<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-row type="flex" justify="end">
        <el-button type="primary" @click="showFn('添加角色')">添加角色</el-button>
      </el-row>
      <br>
      <el-card class="box-card">
        <el-table
          :data="tableData"
          style="width: 100%"
          :row-key="tableData.date"
          :tree-props="{children: 'permissions'}"
        >
          >
          <el-table-column
            prop="title"
            label="角色名称"
          />
          <el-table-column
            prop="description"
            label="角色描述"
          />
          <el-table-column
            label="操作"
            width="300"
          >
            <template v-slot="{row}">
              <el-button
                type="primary"
                size="mini"
                @click="fenpei(row.id)"
              >分配权限</el-button>
              <el-button
                size="mini"
                @click="showFn('编辑角色',row.id)"
              >编辑</el-button>
              <el-button size="mini" type="danger" @click="del(row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
        <br>
        <!-- //分页器 -->
        <el-row type="flex" justify="center">
          <el-pagination
            :current-page="page"
            :page-sizes="[8, 16, 24, 32]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
          <!-- <el-pagination layout="total" :total="total" /> -->
        </el-row>
      </el-card>
      <!-- 添加,编辑 -->
      <el-dialog :title="title" :visible="dialogShow" :before-close="cloFn">
        <el-form ref="jueseForm" :model="form">
          <el-form-item label="角色名称" prop="name" :label-width="formLabelWidth">
            <el-input v-model="form.title" autocomplete="off" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description" :label-width="formLabelWidth">
            <el-input v-model="form.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="butOk">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog
        title="权限列表"
        :visible="quanxianVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-checkbox-group v-model="checklist">
          <el-tree
            :data="quanxianData"
            :props="{label: 'title',children: 'children'}"
            default-expand-all
            :expand-on-click-node="false"
          >
            <template #default="{data}">
              <el-checkbox :label="data.id">
                {{ data.title }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="quanxinaOk">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getJueseList, getJueseCount, addJuese, getJueseById, editJuese, delJueseById } from '@/api/juese'
import { getPermissions } from '@/api/quanxian'
import { listToTree } from '@/utils'
export default {
  data() {
    return {
      formLabelWidth: '120px',
      dialogShow: false,
      quanxianVisible: false,
      tableData: [],
      form: {},
      quanxianData: [],
      allquanxian: [],
      page: 1,
      size: 8,
      total: 0,
      title: '',
      checklist: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  async created() {
    await this.getJueseListFn(this.size, this.page)
    this.total = await getJueseCount()
    const res = await getPermissions()
    this.allquanxian = res
    this.quanxianData = listToTree(res, '0')
    console.log('权限', this.quanxianData)
  },
  methods: {
    async getJueseListFn(size, page) {
      const res = await getJueseList(size, 0 + size * (page - 1))
      let date = 0
      res.forEach(v => {
        v.date = date++
      })
      this.tableData = res
      console.log('角色数据', this.tableData)
    },
    // 添加编辑对话框
    async showFn(val, id) {
      this.dialogShow = true
      this.title = val
      if (id) {
        this.form = await getJueseById(id)
      }
    },
    async butOk() {
      if (this.title === '添加角色') {
        await addJuese(this.form)
      } else if (this.title === '编辑角色') {
        await editJuese(this.form)
      }
      this.cloFn()
      this.getJueseListFn()
    },
    cloFn() {
      this.dialogShow = false
      this.$refs.jueseForm.resetFields()
      this.form = {}
    },
    // 分配
    async fenpei(id) {
      if (id) {
        this.form = await getJueseById(id)
        this.checklist = this.form.permissions.map(v => v.id)
      }
      this.quanxianVisible = true
    },
    // 权限确定
    async quanxinaOk() {
      this.form.permissions = this.allquanxian.filter(v => this.checklist.indexOf(v.id) !== -1)
      await editJuese(this.form)
      this.handleClose()
      this.getJueseListFn()
    },
    handleClose() {
      this.quanxianVisible = false
    },
    // 删除权限
    async del(id) {
      if (confirm('确定删除吗?')) {
        console.log('删除')
        await delJueseById(id)
        this.getJueseListFn()
      }
    },
    async  handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.size = val
      await this.getJueseListFn(this.size, this.page)
    },
    async  handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      await this.getJueseListFn(this.size, this.page)
    }

  }
}
</script>

<style lang='less' scoped >

</style>
